#include "bsp_temp.h"
#include "esp_log.h"
#include "bsp_param.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
static const char *TAG = "TEMP";
const uint32_t NTC_RES_TABLE[131] = {
	32919, 																				// 0
	31270, 29715, 28246, 26858, 25547, 24307, 23135, 22026, 20977, 19987, 				// 1~10
	19044, 18154, 17310, 16510, 15752, 15034, 14352, 13705, 13090, 12507, 				// 11~20
	11953, 11427, 10927, 10452, 10000, 9570, 9161, 8771, 8401, 8048, 					// 21~30
	7712, 7391, 7086, 6795, 6518, 6254, 6001, 5761, 5531, 5311, 						// 31~40
	5102, 4902, 4710, 4528, 4353, 4186, 4026, 3874, 3728, 3588, 						// 41~50
	3454, 3326, 3203, 3085, 2973, 2865, 2761, 2662, 2567, 2476, 						// 51~60
	2388, 2304, 2223, 2146, 2072, 2000, 1932, 1866, 1803, 1742, 						// 61~70
	1684, 1627, 1573, 1521, 1471, 1423, 1377, 1332, 1289, 1248, 						// 71~80
	1208, 1170, 1133, 1097, 1063, 1030, 998, 968, 938, 909, 							// 81~90
	882, 855, 829, 805, 781, 758, 735, 714, 693,679, 									// 91~100
    659,640,622,605,588,571,555,540,525,510,                                            // 101~110
    496,483,470,457,445,433,421,410,399,389,                                            // 111~120
    379,369,359,350,341,332,324,316,308,300,                                            // 121~130
};
//-------------ADC1 Init---------------//
adc_oneshot_unit_handle_t adc1_handle;
adc_oneshot_unit_init_cfg_t init_config1 = {
    .unit_id = ADC_UNIT_1,
};
//-------------ADC1 Config---------------//
adc_oneshot_chan_cfg_t config = {
    .bitwidth = ADC_BITWIDTH_DEFAULT,
    .atten = EXAMPLE_ADC_ATTEN,
};

void temp_adc_init()
{
    adc_oneshot_new_unit(&init_config1, &adc1_handle);
    adc_oneshot_config_channel(adc1_handle, EXAMPLE_NTC_CAHNNEL, &config);
}


uint8_t temp_read()
{
    uint8_t temp = 25;
    int adc_num;
    int adc_num_average = 0;
    uint32_t read_res;
    for(uint8_t i=0; i<10; i++){
        adc_oneshot_read(adc1_handle, EXAMPLE_NTC_CAHNNEL, &adc_num);
        adc_num_average += adc_num;
    }
    adc_num = adc_num_average/10;
    if(adc_num == 0){
        return NTC_TEMP_ERROR;
    }
    read_res = (adc_num * 10000)/4096.0/(1.0-(adc_num/4096.0));
    if(read_res >= NTC_RES_TABLE[0]){
        return 0;
    }else if(read_res <= NTC_RES_TABLE[130]){
        return 130;
    }
    for(uint8_t i=0; i<=130; i++){
        if(read_res >= NTC_RES_TABLE[i]){
            temp = i;
            break;
        }
    }
    return temp;
}




#define MAX_TEMP 43
void task_temp(void *pvParameters)
{
    uint8_t Read_Temp = 25;
    uint8_t Read_Temp_old = 25;
    uint16_t read_temp_timer = 0;
    sys_main_param_t* sys_temp_param = Get_sys_param_pos();
    for(;;){
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        Read_Temp = temp_read();
        ESP_LOGI(TAG, "READ_TEMP = %d", Read_Temp);
        

        if((Read_Temp >= NTC_TEMP_ERROR) || (Read_Temp == 0)){
            /* 报警 */
            sys_temp_param->sys_error_type = ERROR_TYPE_NTC_ERROR;
            read_temp_timer = 0;
        }else{
            sys_temp_param->sys_error_type = ERROR_TYPE_NO_ERROR;
            read_temp_timer = 0;
        }

        if (sys_temp_param->sys_status == SYS_RUN_NORMAL)
        {
            if(Read_Temp >= MAX_TEMP){
                read_temp_timer = 0;
                sys_temp_param->sys_temp_protect = TEMP_PROTECT_OPEN;
            }else{
                if(read_temp_timer == 0){
                    Read_Temp_old = Read_Temp;
                }
                if((++read_temp_timer) >= 5*60){
                    if((Read_Temp-Read_Temp_old)<=3){
                        sys_temp_param->sys_temp_protect = TEMP_PROTECT_OPEN;
                    }else{
                        sys_temp_param->sys_temp_protect = TEMP_PROTECT_CLOSE;
                    }

                }else{
                    sys_temp_param->sys_temp_protect = TEMP_PROTECT_CLOSE;
                }
            }

        }else{
            read_temp_timer = 0;
        }
        

    }


}































































